﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using TreaShares.Backoffice;
using TreaShares.Backoffice.Controlers;

namespace TreaShares.Backoffice.Administrator
{
    public partial class ManageUsers : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void btnCreateUser_Click(object sender, EventArgs e)
        {

        }

        protected void lbCreateUser_Click(object sender, EventArgs e)
        {

        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            try
            {
                Membership.CreateUser(this.txtUserName.Text, this.txtPassword.Text, this.txtEmail.Text);
                UsersManager.UpdateUserOrganisationId(this.txtUserName.Text, this.Master.OrgBasicInfo.Id);
                Roles.AddUserToRole(this.txtUserName.Text, this.rbRoles.SelectedValue);
                this.GridView1.DataBind();
            }
            catch (Exception ex)
            {
                this.ErrorMessage.Text = ex.Message;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int rowNumber = Convert.ToInt32(e.CommandArgument);
                var dic = ((GridView)e.CommandSource).DataKeys[rowNumber].Values;
                string userName = dic["UserName"].ToString();
                string roleName = dic["RoleName"].ToString();
                this.LoginUser(userName);
                Response.Redirect(string.Format("~/Backoffice/{0}/default.aspx", roleName));
            }
        }

        private void LoginUser(string logInAsUserName)
        {
            //var a = Membership.ValidateUser("Administrator", "danielu1121");

            //var b = Roles.IsUserInRole("Administrator", "Administrator");
            // Create the cookie that contains the forms authentication ticket
            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(logInAsUserName, false);

            // Get the FormsAuthenticationTicket out of the encrypted cookie
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

            // Create a new FormsAuthenticationTicket that includes our custom User Data
            FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate,
                ticket.Expiration, ticket.IsPersistent, "Pini");

            // Update the authCookie's Value to use the encrypted version of newTicket
            authCookie.Value = FormsAuthentication.Encrypt(newTicket);

            //' Manually add the authCookie to the Cookies collection 
            Response.Cookies.Add(authCookie);

            Response.Cookies["OrganisationId"].Value = Master.OrgBasicInfo.Id.ToString();
            Response.Cookies["OrganisationId"].Expires = DateTime.Now.AddDays(5);
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Alternate)
                {
                    e.Row.Attributes.Add("onmouseover", "this.className='SelectedRowStyle'");
                    e.Row.Attributes.Add("onmouseout", "this.className='AlternatingRow'");
                }
                else
                {
                    e.Row.Attributes.Add("onmouseover", "this.className='SelectedRowStyle'");
                    e.Row.Attributes.Add("onmouseout", "this.className='RowBackColor'");
                }
            }
        }
    }
}
